require 'bdb'

#Node = Struct.new("Node", :path, :value, :stamp)

class Node
  attr_accessor :path, :value, :stamp

  def initialize(path, value, stamp)
    @path = path
    @value = value
    @stamp = stamp
  end
end

db = BDB::Btree.open "db/nodes", nil, BDB::CREATE | BDB::TRUNCATE

aux = BDB::Btree.open "db/nodes.stamp", nil, BDB::CREATE | BDB::TRUNCATE,
   "set_flags" => BDB::DUPSORT
   
db.associate(aux) {|aux, k, v| Marshal.load(v).stamp}

################################################################################

x = 100_000
def r; rand(1_000_000_000); end

################################################################################

t1 = Time.now
x.times do
  n = Node.new("/cex1/r#{r}", r, Time.now - r)
  db[n.path] = Marshal.dump(n)
end
t2 = Time.now
e = t2 - t1
t_per_n = x / e

puts "writing #{x} records took #{e} seconds. #{t_per_n} records per second."

################################################################################

t1 = Time.now
x = 0
db.each do |k, v|
  Marshal.load(v).path
  x += 1
end
t2 = Time.now
e = t2 - t1
t_per_n = x / e

puts "reading #{x} records took #{e} seconds. #{t_per_n} records per second."

################################################################################

t1 = Time.now
x = 0
aux.each do |k, v|
  Marshal.load(v).path
  x += 1
end
t2 = Time.now
e = t2 - t1
t_per_n = x / e

puts "sdb: reading #{x} records took #{e} seconds. #{t_per_n} records per second."

